package com.zgz.underwrite.dao.impl;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.zgz.underwrite.dao.BaseDao;

public class BaseDaoImpl<T> extends HibernateDaoSupport implements BaseDao<T>{
	
	public Class<T> entityClass;
	
	@SuppressWarnings("unchecked")
	public BaseDaoImpl(){
		Type genType = getClass().getGenericSuperclass();
		Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
		entityClass = (Class<T>)params[0];
	}
	
	@Override
	public void delete(Object entity) {
		getHibernateTemplate().delete(entity);
	}

	@Override
	public T get(Serializable pk) {
		return super.getHibernateTemplate().get(entityClass, pk);
	}

	@Override
	public List<T> getAll() {
		return super.getHibernateTemplate().loadAll(entityClass);
	}

	@Override
	public void save(Object entity) {
		super.getHibernateTemplate().save(entity);
	}

	@Override
	public void update(Object entity) {
		super.getHibernateTemplate().update(entity);
	}

	@SuppressWarnings("unchecked")
	public List<T> findHql(String queryString){
		return super.getHibernateTemplate().find(queryString);
	}
	
	@SuppressWarnings("unchecked")
	public List<T> findByCriteria(DetachedCriteria criteria){
		return super.getHibernateTemplate().findByCriteria(criteria);
	}

	@Override
	public void deleteAll(Collection c) throws Exception {
		super.getHibernateTemplate().deleteAll(c);
	}
	

}
